using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace NTTHAO.CommonWeb
{
    /// <summary>
    /// Summary description for CookieUtility
    /// </summary>
    public static class CookieUtility
    {
        public static bool SupportCookie(HttpRequest request)
        {
            return request.Browser.Cookies;
        }

        public static void SetCookie(string key, string value, DateTime timeout, HttpRequest request, HttpResponse response)
        {
            if (!SupportCookie(request))
            {
                return;
            }
            if (request.Cookies[key] != null)
            {
                request.Cookies[key].Value = value;
                request.Cookies[key].Expires = timeout;

                response.Cookies[key].Value = value;
                response.Cookies[key].Expires = timeout;
            }
            else
            {
                HttpCookie myCookie = new HttpCookie(key);
                myCookie.Value = value;
                myCookie.Expires = timeout;
                request.Cookies.Add(myCookie);
                response.Cookies.Add(myCookie);
            }
        }

        public static string GetCookieValue(string key, HttpRequest request)
        {
            if (!SupportCookie(request))
            {
                return null;
            }
            if (request.Cookies[key] != null)
            {
                return request.Cookies[key].Value;
            }
            else
            {
                return null;
            }
        }

        public static void DeleteCookie(string key, HttpRequest request, HttpResponse response)
        {
            if (!SupportCookie(request))
            {
                return;
            }
            if (request.Cookies[key] != null)
            {
                response.Cookies[key].Expires = DateTime.Now.AddDays(-1);
            }
        }
    }
}